package com.dxp.config;

import com.alibaba.fastjson2.JSON;
import com.dxp.common.BaseContext;
import com.dxp.common.R;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;


@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       Long employee = (Long) request.getSession().getAttribute("employee");
        Long user = (Long) request.getSession().getAttribute("user");
        if(employee != null) {
            BaseContext.setCurrentId(employee);
            return true;
        }else if(user != null) {
            BaseContext.setCurrentId(user);
            return true;
        }else {
            System.out.println("saaaaaas");
            response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
            System.out.println("拦截到请求:"+request.getRequestURI());
            return false;
        }


    }
}
